# Contributor: Sheila Aman <sheila@vulpine.house>
# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=protobuf
_gemname=google-protobuf
pkgver=3.21.2
_tstver=5ec7f0c4a113e2f18ac2c6cc7df51ad6afc24081
pkgrel=0
pkgdesc="Library for extensible, efficient structure packing"
url="https://github.com/google/protobuf"
arch="all"
license="BSD-3-Clause"
depends="
	protoc=$pkgver-r$pkgrel
	libprotoc=$pkgver-r$pkgrel
	libprotobuf=$pkgver-r$pkgrel
	libprotobuf-lite=$pkgver-r$pkgrel
	"
depends_dev="zlib-dev"
makedepends="
	$depends_dev
	autoconf
	automake
	libtool
	ruby
	ruby-dev
	ruby-rake
	"
subpackages="
	ruby-$_gemname:_ruby
	$pkgname-dev
	$pkgname-vim::noarch
	protoc
	libprotoc
	libprotobuf
	libprotobuf-lite
	"
# googletest 1.8 is not enought for protobuf
# https://github.com/google/googletest/issues/2267
source="$pkgname-$pkgver.tar.gz::https://github.com/protocolbuffers/protobuf/archive/v$pkgver.tar.gz
	googletest-$_tstver.tar.gz::https://github.com/google/googletest/archive/$_tstver.tar.gz
	ruby-fix-cflags.patch
	skip-failing-tests.patch
	trim-rakefile.patch
	"

prepare() {
	default_prepare

	./autogen.sh

	# symlink tests to the test directory
	rm -rf third_party/googletest
	ln -sf "$srcdir"/googletest-$_tstver \
		"$builddir"/third_party/googletest
}

build() {
	CXXFLAGS="$CXXFLAGS -fno-delete-null-pointer-checks -Wno-error" \
		./configure --prefix=/usr \
			--sysconfdir=/etc \
			--mandir=/usr/share/man \
			--infodir=/usr/share/info \
			--localstatedir=/var
	make

	cd "$builddir"/ruby

	# Generate proto files for built-in protocols.
	rake genproto

	gem build $_gemname.gemspec
	gem install --local \
		--install-dir dist \
		--ignore-dependencies \
		--no-document \
		--verbose \
		$_gemname

	# build test-suite
	local test; for test in googletest googlemock; do
		cd "$builddir/third_party/googletest/$test"
		autoreconf -vfi
		./configure
		make
	done
}

# TODO: Run tests for ruby gem.
check() {
	make check
}

package() {
	make DESTDIR="$pkgdir" install
}

_ruby() {
	depends=""
	pkgdesc="Ruby bindings to Google's data interchange format"

	local gemdir="$subpkgdir/$(ruby -e 'puts Gem.default_dir')"
	cd "$builddir"/ruby/dist

	mkdir -p "$gemdir"
	cp -r extensions gems specifications "$gemdir"/

	# Remove duplicated .so libs (should be only in extensions directory).
	find "$gemdir"/gems/ -name "*.so" -delete

	# Remove unnecessary files.
	cd "$gemdir"/gems/$_gemname-$pkgver
	rm -r ext/ tests/
}

vim() {
	pkgdesc="Vim syntax for $pkgname"
	depends=""
	install_if="$pkgname=$pkgver-r$pkgrel vim"

	install -Dm644 "$builddir"/editors/proto.vim \
		"$subpkgdir"/usr/share/vim/vimfiles/syntax/proto.vim
}

libprotoc() {
	depends=""
	pkgdesc="Runtime library for Protocol Buffer compiler"

	mkdir -p "$subpkgdir"/usr/lib
	mv "$pkgdir"/usr/lib/libprotoc*.so.* "$subpkgdir"/usr/lib
}

protoc() {
	depends=""
	pkgdesc="Protocol buffer compiler binary and library"

	mkdir -p "$subpkgdir"/usr/bin
	mv "$pkgdir"/usr/bin/protoc "$subpkgdir"/usr/bin
}

libprotobuf() {
	depends=""
	pkgdesc="Runtime library for C++ users of protocol buffers"

	mkdir -p "$subpkgdir"/usr/lib
	mv "$pkgdir"/usr/lib/libprotobuf.so.* "$subpkgdir"/usr/lib
}

lite() {
	depends=""
	pkgdesc="Runtime library for C++ users with 'lite runtime' setting of protocol buffers"

	mkdir -p "$subpkgdir"/usr/lib
	mv "$pkgdir"/usr/lib/libprotobuf-lite.so.* "$subpkgdir"/usr/lib
}

dev() {
	depends_dev="$depends_dev protoc=$pkgver-r$pkgrel"
	default_dev
}

sha512sums="
c08b090d9c447667c5b166260b6c4db88c3dfdf421182fb6fa20619b82a62eba47434735431fd6854f1121f8e5fdc11e5ee72ce30774eee0e02c1a3a3b85c4f3  protobuf-3.21.2.tar.gz
623b077b3334958fafcbc34aa85891883277994af33be530efd903f47738a3e3562001cbf3b6da1a5e7d03803c5bd51bcc1fab81490db85d5a4f2b15e7de1495  googletest-5ec7f0c4a113e2f18ac2c6cc7df51ad6afc24081.tar.gz
d5f482acdb0b4f48e88b70a14cb343bea177875f2125065c760d02c89a7658e55a82c531b5a5a0cc5270ae186cd01ccd17110d2d22dbc48a2f1240b3148ff3cb  ruby-fix-cflags.patch
1bd542ea4d4b605bf8396769ddc65f86f13f214bb4a58ece4494d5451bd9ae547041af28dd500784f73d4417664466d2b031a81aa96eb6f0877c253731eb8e6d  skip-failing-tests.patch
444c018aaaa07958f8ec2b62ece281439cb0e3340c3d0bc19ed5bf29f9ff415c08777a7106e2f908e6bbbcca5288c1ddd9c791438390038d5e0f3ffdd08773be  trim-rakefile.patch
"
